cmake_minimum_required(VERSION 3.16)
project(UdpMsgQue VERSION 1.0 LANGUAGES C CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Set up AUTOMOC and some sensible defaults for runtime execution
# When using Qt 6.3, you can replace the code block below with
# qt_standard_project_setup()
set(CMAKE_AUTOMOC ON)
include(GNUInstallDirs)
set(CMAKE_AUTOUIC ON)

set(QT5_32_DIR C:/Qt/5.15.1/msvc2019)
set(QT5_64_DIR C:/Qt/5.15.1/msvc2019_64)
set(QT6_64_DIR C:/Qt/6.6.2/msvc2019_64)

# 忽略系统环境变量，因为默认的qt路径可能与指定的x64/32有冲突
set(ENV{PATH} "")

set(QT5_32_CONFIG_DIR "${QT5_32_DIR}/lib/cmake/Qt5")
set(QT5_64_CONFIG_DIR "${QT5_64_DIR}/lib/cmake/Qt5")

message("QQQQQQQQQQQQQQQQQQQQ ${QT5_32_CONFIG_DIR}")

# set(QT_DIR ${QT5_64_CONFIG_DIR})
# set(QT5_DIR ${QT5_64_CONFIG_DIR})
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    # 设置64位编译时使用的变量
    set(ARCH "64")

    set(CMAKE_PREFIX_PATH ${QT5_64_DIR})
    set(QTDIR ${QT5_64_DIR})

    message(STATUS "Building for 64-bit target")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    set(ARCH "32")

    set(CMAKE_PREFIX_PATH ${QT5_32_DIR})
    set(QTDIR ${QT5_32_DIR})

    message(STATUS "Building for 32-bit target")
else()
    message(WARNING "Unknown architecture, neither 32-bit nor 64-bit")
endif()




find_package(QT NAMES Qt5 Qt6 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Gui)
find_package(Qt${QT_VERSION_MAJOR} OPTIONAL_COMPONENTS Widgets)

add_executable(UdpMsgQue WIN32 MACOSX_BUNDLE
    MainWindow.cpp MainWindow.h MainWindow.ui
    Udp.cpp Udp.h
    main.cpp
)
target_compile_definitions(UdpMsgQue PRIVATE
    QT_DEPRECATED_WARNINGS
)

target_link_libraries(UdpMsgQue PRIVATE
    Qt::Core
    Qt::Gui
)

if((QT_VERSION_MAJOR GREATER 4))
    target_link_libraries(UdpMsgQue PRIVATE
        Qt::Widgets
    )
endif()

if(WIN32)
    target_link_libraries(UdpMsgQue PRIVATE
        WS2_32
    )
    find_program(WINDEPLOYQT_EXECUTABLE NAMES windeployqt HINTS ${QTDIR} ENV QTDIR PATH_SUFFIXES bin)
    add_custom_command(TARGET UdpMsgQue POST_BUILD
        COMMAND ${WINDEPLOYQT_EXECUTABLE} $<TARGET_FILE:UdpMsgQue>
        COMMENT "Running windeployqt...")
endif()

install(TARGETS UdpMsgQue
    BUNDLE DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# Consider using qt_generate_deploy_app_script() for app deployment if
# the project can use Qt 6.3. In that case rerun qmake2cmake with
# --min-qt-version=6.3.
